package com.website.view.menu
{
	import com.bakh.picWall.Bakh3DWall;
	import com.greensock.TweenMax;
	import com.website.data.ProjectsXml;
	import com.website.util.Constant;
	import com.website.util.DisplayUtil;
	import com.website.view.HomePage;
	import com.website.view.LayerManager;
	import com.website.view.component.BakhTextFiled;
	import com.website.view.component.ClickBtnComp;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class MenuComponent extends Sprite
	{
		public var navArray:Array;
		private var _hoverBar:Sprite;
		private var _bar:Sprite;
		private var _btn0:Sprite;
		private var _btn0BG:Sprite;
		private var _btn0Field:TextField;
		private var _fadeSpeed:int;
		private var currNavId:int;
		
		private var _naveComp:NavComponent;
		private var _parter:ClickBtnComp;
		private var homePage:HomePage = new HomePage();
		public function MenuComponent()
		{
			_fadeSpeed = 0.5;
			navArray = new Array(["Projects"], ["Practice"], ["Contact"], ["News"],[""], ["En|中"]);
			_parter = new ClickBtnComp("We are partner of Richard Meier",Constant.hotColor,14,"georgia");
			_parter.addEventListener(MouseEvent.CLICK, onParterClick);
			buildBar();
			_naveComp = new NavComponent();
			addChild(_naveComp);
			addChild(homePage);
			addChild(_parter);
			doResize();
		}
		
		public function get naveComp():NavComponent
		{
			return _naveComp;
		}

		private function buildBar() : void
		{
			var textFormat:TextFormat = undefined;
			_hoverBar = new Sprite();
			_hoverBar.graphics.beginFill(16777215, 1);
			_hoverBar.graphics.drawRect(0, 0, 999, 28);
			_hoverBar.graphics.endFill();
			addChild(_hoverBar);
			_bar = new Sprite();
			_bar.graphics.beginFill(0x777777, 1);
			_bar.graphics.drawRect(0, 0, 999, 28);
			_bar.graphics.endFill();
			addChild(_bar);
			textFormat = new TextFormat();
			//            textFormat.font = new BodoniEmbed().fontName;
			textFormat.size = 17;
			textFormat.letterSpacing = -0.6;
			textFormat.leftMargin = 0;
			textFormat.rightMargin = 0;
			textFormat.color = "0xFFFFFF";
			_btn0 = new Sprite();
			_btn0.useHandCursor = true;
			_btn0.mouseEnabled = true;
			_btn0.buttonMode = true;
			_btn0.mouseChildren = true;
			_btn0.addEventListener(MouseEvent.MOUSE_OVER, _btn0RollOver, false, 0, true);
			_btn0.addEventListener(MouseEvent.MOUSE_OUT, _btn0RollOut, false, 0, true);
			_btn0.addEventListener(MouseEvent.CLICK, _btn0Click, false, 0, true);
			_btn0BG = new Sprite();
			_btn0BG.graphics.beginFill(16777215, 0);
			_btn0BG.graphics.drawRect(0, 0, 1, 1);
			_btn0BG.graphics.endFill();
			_btn0.addChild(_btn0BG);
			_btn0Field = new TextField();
			_btn0Field.name = "btn0Field";
			_btn0Field.autoSize = "left";
			_btn0Field.antiAliasType = AntiAliasType.ADVANCED;
			_btn0Field.gridFitType = GridFitType.PIXEL;
			_btn0Field.selectable = false;
//			_btn0Field.embedFonts = true;
			_btn0Field.text = "BAKH-architecture";
			_btn0Field.setTextFormat(textFormat);
			_btn0Field.alpha = 1;
			_btn0Field.x = 0;
			_btn0Field.y = -1;
			_btn0.addChild(_btn0Field);
			addChild(_btn0);
			var index:int = 0;
			var btn:MenuBtn;
			while (index < navArray.length)
			{
				
				btn = new MenuBtn(navArray[index][0]);
                btn.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver, false, 0, true);
				btn.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut, false, 0, true);
				btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
				btn.idNum = index;
				btn.name = "btn" + index;
				btn.mouseEnabled = true;
				btn.buttonMode = true;
				btn.mouseChildren = true;
				navArray[index].push(btn);
				addChild(btn);
				index++;
			}
		}

		private function btnRollOver(event:MouseEvent) : void
		{
			 
			TweenMax.to(event.currentTarget.field, _fadeSpeed, {tint:16777215, overwrite:true});
		}
		
		private function btnRollOut(event:MouseEvent) : void
		{
			
			TweenMax.to(event.currentTarget.field, _fadeSpeed, {tint:12500670, overwrite:true});
		}

		private function _btn0Click(event:MouseEvent):void
		{
			
		}

		private function _btn0RollOut(event:MouseEvent):void
		{
			
		}

		private function _btn0RollOver(event:MouseEvent):void
		{
			
		}

		private function btnClick(event:MouseEvent) : void
		{
			DisplayUtil.removeFromParent(homePage);
			DisplayUtil.removeFromParent(_parter);
			currNavId = event.currentTarget.idNum;
			switchCols(event.currentTarget.idNum);
			TweenMax.to(event.currentTarget.field, _fadeSpeed, {tint:16777215, overwrite:true});
			dispatchEvent(new Event("menuComponentClick"));
		}
		
		public function switchCols(id:Number) : void
		{
			_naveComp.switchCols(id);
		}
		public function autoSwitch(id:Number)
		{
			switchCols(id);
		}
		
		private function doResize() : void
		{
			var _loc_2:int = 0;
			_bar.width = Math.round(Constant.headerW);
			_bar.height = Math.round(Constant.headerH);
			_hoverBar.width = _bar.width;
			_hoverBar.height = _bar.height;
			_btn0.x = 0;
			_btn0.y = 2 * Constant.unitW / 75;
			_btn0BG.y = -2;
			_btn0BG.width = 4 * (Constant.unitW + Constant.gutterW);
			_btn0BG.height = _bar.height;
			var posArr:Array = [
				4 * (Constant.unitW + Constant.gutterW), 
				5 * (Constant.unitW + Constant.gutterW), 
				6 * (Constant.unitW + Constant.gutterW), 
				7 * (Constant.unitW + Constant.gutterW), 
				8 * (Constant.unitW + Constant.gutterW), 
				10 * (Constant.unitW + Constant.gutterW)];
			var i:int = 0;
			while (i < navArray.length)
			{
				navArray[i][1].x = Math.round(posArr[i]);
				navArray[i][1].y = 2;
				i++;
			}
			_naveComp.y  = - (height);
			homePage.x = (Constant.gridCurrW - homePage.width)  /2;
			homePage.y = -(Math.round(Constant.gridY + Constant.gridCurrH)-200);
			_parter.y = _naveComp.y - _parter.height;
//			var _loc_3:* = Constant.unitW / 75;
//			logoSmall.scaleY = Constant.unitW / 75;
//			logoSmall.scaleX = _loc_3;
//			logoSmall.width = Math.round(logoSmall.width);
//			logoSmall.height = Math.round(logoSmall.height);
//			hRule1.x = Math.round(4 * (Constant.unitW + Constant.gutterW));
//			hRule1.y = Math.round(bar.height + Constant.unitH + 4 / 9 * Constant.gutterH);
//			hRule1.width = Math.round(8 * Constant.unitW + 7 * Constant.gutterW);
//			hRule1.height = 1;
//			hRule2.y = Math.round(bar.height + 2 * Constant.unitH + Constant.gutterH);
//			hRule2.width = Math.round(Constant.gridCurrW);
//			hRule2.height = 1;
//			hRule3.y = Math.round(Constant.gridCurrH);
//			hRule3.width = Math.round(Constant.gridCurrW);
//			hRule3.height = 1;
		}
		
		private function onParterClick(ev:MouseEvent):void
		{
			navigateToURL(new URLRequest("http://www.richardmeier.com/www/"),"_blank");
		}

	}
}